var express = require('express');
var router = express.Router();
const Cost = require('../dao/cost_dao');
const comBase = require("../dao/com_base_dao");
/**
 * 为用户增加一条成本记录
 */
router.post('', async function (req, res, next) {
    const resData = {
        code: 0,
        msg: 'success',
        data: []
    }
    console.log('body', req.body)
    resData.data = await Cost.add({
        stock_code: req.body.stock_code,
        user_id: req.token.id,
    })
    res.json(resData).end();
});


/**
 * 获取用户持股成本列表
 */
router.get('/list/:offset/:limit', async function (req, res, next) {
    const resData = {
        code: 0,
        msg: 'success',
        data: []
    }
    const offset = req.params.offset
    const limit = req.params.limit
    const user_id = req.token.id
    resData.data = await Cost.findList(parseInt(user_id), parseInt(offset), parseInt(limit))
    res.json(resData).end();
});
router.get('/:id', async function (req, res, next) {
    const resData = {
        code: 0,
        msg: 'success',
        data: []
    }
    const id = req.params.id
    const user_id = req.token.id
    resData.data = await Cost.findOne(parseInt(id), user_id)
    res.json(resData).end();
});
router.put('', async function (req, res, next) {
    const resData = {
        code: 0,
        msg: 'success',
        data: []
    }
    const user_id = req.token.id
    resData.data = await Cost.update(req.body, user_id)
    res.json(resData).end();
});
router.delete('/:id', async function (req, res, next) {
    const resData = {
        code: 0,
        msg: 'success',
        data: []
    }
    const id = req.params.id
    const user_id = req.token.id
    console.log('delete', id, 'user_id', user_id)
    resData.data = await Cost.deleteById(parseInt(id), user_id)
    res.json(resData).end();
});
module.exports = router;
